package com.leji.song.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.dgl.sdk.util.DBStore;
import com.dgl.sdk.view.WidgetAnim;
import com.leji.song.R;
import com.leji.song.extra.BaseActivity;
import com.leji.song.mine.model.DS_Address;
import com.leji.song.util.ConstAns;

import java.util.UUID;

public class AddAddressActivity extends BaseActivity{

    private Button saveAddress;

    private EditText contactPeople, phoneNum, address;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setAnimType(RIGHT_ENTER_LEFT_EXT);
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.add_adress);
        initView();

        initAdress();
	}

    private void initAdress() {

        Intent intent = getIntent();

        if(intent!= null){
            address.setText(intent.getStringExtra("address"));
        }

    }

    private void initView() {
        setActivityTitle("新建送餐地址");
        setReturnBtn();


        saveAddress = (Button) findViewById(R.id.saveAddress);
        contactPeople = (EditText) findViewById(R.id.contactPeople);
        phoneNum = (EditText) findViewById(R.id.phoneNum);
        address = (EditText) findViewById(R.id.address);

        saveAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                addAddress();

                showToast("保存地址成功！");
                finish();
            }
        });
    }

    private void addAddress() {
        if(WidgetAnim.checkIfEmpty(this, contactPeople)){
            return;
        }

        if(WidgetAnim.checkIfEmpty(this, phoneNum)){
            return;
        }

        if(WidgetAnim.checkIfEmpty(this, address)){
            return;
        }

        DS_Address myAddress = new DS_Address();
        myAddress.setName(contactPeople.getText().toString());
        myAddress.setPhoneNum(phoneNum.getText().toString());
        myAddress.setAdress(address.getText().toString());
        myAddress.setAddressID(UUID.randomUUID().toString());

        DBStore.save(myAddress , ConstAns.DB_TABLE_MY_ADDRESS);
    }

}
